home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Science / RLaB / rlib / eye.r < prev    next >
Text File  |  1994-04-25  |  1KB  |  50 lines

  1. //-------------------------------------------------------------------//
  2. // 
  3. //  Syntax:    eye ( S1 , S2 )
  4. //        eye ( A )
  5. //
  6. //  Description:
  7.  
  8. //  Create an identity matrix with number of rows and columns
  9. //  specified by two scalars (S1 - rows, and S2 - columns).
  10.  
  11. //  If the input is A, a matrix, with two elements, then the 1st
  12. //  element of A determines the number of rows, and the 2nd element of
  13. //  A determines the number of columns of the new identity matrix.
  14.  
  15. //  The matrix input option exists so that:
  16. //
  17. //        eye( size( X ) )
  18. //  will work.
  19.  
  20. //  See Also: ones, zeros
  21. //-------------------------------------------------------------------//
  22.  
  23. eye = function( m , n ) 
  24. {
  25.   local(i, N, new);
  26.  
  27.   if (!exist (n))
  28.   {
  29.     if(m.n != 2) { error("only 2-el MATRIX allowed as eye() arg"); }
  30.     new = zeros (m[1], m[2]);
  31.     N = min ([m[1], m[2]]);
  32.   else
  33.     if (class (m) == "string" || class (n) == "string") {
  34.       error ("eye(), string arguments not allowed");
  35.     }
  36.     if (max (size (m)) == 1 && max (size (n)) == 1)
  37.     {
  38.       new = zeros (m[1], n[1]);
  39.       N = min ([m[1], n[1]]);
  40.     else
  41.       error ("matrix arguments to eye() must be 1x1");
  42.     }
  43.   }
  44.   for(i in 1:N)
  45.   {
  46.     new[i;i] = 1.0;
  47.   }
  48.   return new;
  49. };
  50.